package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.SongDescAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.Desc;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SongDescFragment extends Fragment {
    private View view;
    private Artist artist;
    private RecyclerView recycleview_desc;
    private SongDescAdapter adapter;
    private List<Desc.IntroductionBean> introductionBeanList = new ArrayList<>();

    public SongDescFragment() {

    }

    public static SongDescFragment newInstance(Bundle bundle) {
        SongDescFragment fragment = new SongDescFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_desc, container, false);
        artist = (Artist) getArguments().getSerializable("singer");
        init();
        return view;
    }




    private void init() {
        recycleview_desc = view.findViewById(R.id.recycleview_desc);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        recycleview_desc.setLayoutManager(manager);
        adapter = new SongDescAdapter(getContext());
        recycleview_desc.setAdapter(adapter);
        loadData();
    }

    private void loadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<Desc> call = service.getDesc(artist.getId());
        call.enqueue(new Callback<Desc>() {
            @Override
            public void onResponse(Call<Desc> call, Response<Desc> response) {
                Desc baseResponse = response.body();
                adapter.setData(baseResponse);
            }

            @Override
            public void onFailure(Call<Desc> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
